<?php
class ZipCodeModel extends CI_Model
{
    const collection = 'zipcode';
    
    // Cache
    const cache_enable = true;
    const cache_ttl = 86400;
    const prefix_cache_item = 'zipcode.%s';
    const prefix_cache_list = 'zipcode.list.%s';
    const prefix_cache_count = 'zipcode.count.%s';

    public function  __construct()
    {
        parent::__construct();
    }

    public function initData($data)
    {
        // Check $data
        if(!is_array($data)) return null;

        // Define fields
        $fields = array('_id', 'state_id', 'city_id', 'lat', 'long', 'params');

        $list = null;

        foreach($fields as $key) {
            if(isset($data[$key])) {
                $list[$key] = $data[$key];
            }
        }

        // Return
        return $list;
    }

    public function insert($data)
    {
        // Init data
        $data = $this->initData($data);

        // Check empty
        if(empty($data)) return false;

        // Insert into Mongo
        $rs = $this->mongo_db->insert(self::collection, $data);

        // Return
        return $rs;
    }

    public function update($data)
    {
        // Init data
        $data = $this->initData($data);

        if(empty($data['_id'])) return false;

        $rs = $this->mongo_db->update(self::collection, $data);

        // Return
        return $rs;
    }

    public function delete($zipCodeId)
    {
        // Check zipcodeId
        if(empty($zipCodeId)) return false;

        // Delete from mongodb
        $rs = $this->mongo_db->where(array('_id' => $zipCodeId))->delete(self::collection);

        // Return
        return $rs;
    }

    public function selectZipCodeById($zipCodeId)
    {
        // Check zipcodeId
        if(empty($zipCodeId)) return false;

        // Get from mongodb
        $rs = $this->mongo_db->where(array('_id' => $zipCodeId))->getOne(self::collection);

        // Return
        return $rs;
    }

    public function selectZipCodeByState($stateId, $page, $limit)
    {
        // Check stateId
        if(empty($stateId)) return false;

        // Set offset
        $offset = ($page - 1)*$limit;

        $list = $this->mongo_db->where(array('state_id' => $stateId))->offset($offset)->limit($limit)->get(self::collection);
        
        if(is_array($list)) {

            // Json decoder
            foreach($list as &$item)
            {
                if(isset($item['params'])) {
                    $item['params'] = json_decode($item['params']);
                }
            }
        }

        // Return
        return $list;
    }

    public function selectZipCodeByCity($stateId, $cityId, $page, $limit)
    {
        // Check stateId
        if(empty($stateId) || empty($cityId)) return false;

        // Set offset
        $offset = ($page - 1)*$limit;

        $list = $this->mongo_db->where_and(array('state_id' => $stateId, 'city_id' => $cityId))->offset($offset)->limit($limit)->get(self::collection);

        if(is_array($list)) {

            // Json decoder
            foreach($list as &$item)
            {
                if(isset($item['params'])) {
                    $item['params'] = json_decode($item['params']);
                }
            }
        }

        // Return
        return $list;
    }
}
?>